// pages/gas-path/index.js
const app = getApp()
let alarmContent = []
Page({

    /**
     * 页面的初始数据
     */
    data: {
        deviceId: null,
        device: {},
        alarmContent: [],
        children: {},
        childrenVO: {},
        gasRoom: [],
        gasCabinet: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let deviceId = options.deviceId
        if (deviceId != null) {
            this.setData({
                deviceId: deviceId
            })
            app.api.getDeviceDetail(deviceId).then(res => {
                if (res.data != null) {
                    if (res.data.alarmContent != null && res.data.alarmContent.length > 0) {
                        alarmContent = res.data.alarmContent
                    }
                    this.f1(res.data)
                    let _gasRoom = [];
                    let _gasCabinet = []
                    if (res.data.childrenVO != null && res.data.childrenVO.length > 0) {
                        for (let i = 0; i < res.data.childrenVO.length; i++) {
                            let d = res.data.childrenVO[i]
                            if (d.deviceName != null && d.deviceName.indexOf("间") != -1) {
                                _gasRoom.push(d)
                            } else if (d.deviceName != null && d.deviceName.indexOf("柜") != -1) {
                                _gasCabinet.push(d)
                            }
                        }
                    }
                    for (let i = 0; i < _gasCabinet.length; i++) {
                        let d = _gasCabinet[i]
                        if (d.childrenVO != null && d.childrenVO.length > 0) {
                            let _childrenVO = []
                            for (let j = 0; j < d.childrenVO.length; j++) {
                                let c = d.childrenVO[j];
                                let deviceName = c.deviceName;
                                if (deviceName != null) {
                                    let names = deviceName.split("-");
                                    if (names.length > 1) {
                                        let sort = this.getNumber(names[0]);
                                        let name = names[1];
                                        c.deviceName = name;
                                        _childrenVO[sort - 1] = c;
                                    }
                                }
                            }
                            _gasCabinet[i].childrenVO = _childrenVO;
                        }
                    }
                    this.setData({
                        device: res.data,
                        alarmContent: alarmContent,
                        children: res.data.children,
                        childrenVO: res.data.childrenVO,
                        gasRoom: _gasRoom,
                        gasCabinet: _gasCabinet
                    })
                    wx.setNavigationBarTitle({
                        title: res.data.deviceName
                    })
                }
            })
        }
    },

    f1(device) {
        if (device != null && device.childrenVO != null && device.childrenVO.length > 0) {
            for (let i = 0; i < device.childrenVO.length; i++) {
                const d = device.childrenVO[i];
                if (d.alarmContent != null && d.alarmContent.length > 0) {
                    alarmContent.push(d.alarmContent)
                }
                this.f1(d)
            }
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    getNumber(str) {
        var reg = /[\u4e00-\u9fa5]/g;
        let num = str.replace(reg, "");
        return parseInt(num);
    }
})